package com.jung.customer.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.hp.util.Page;
import com.hp.util.PageContext;
import com.hp.xquery.SimpleQuery;
import com.jung.customer.dao.CustomerDao;
import com.jung.customer.dao.CustomerHibernateDao;
import com.jung.customer.model.Customer;
import com.jung.customer.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

	private CustomerDao customerDao;
	private CustomerHibernateDao customerHibernateDao;
	@Resource
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	@Resource
  public void setCustomerHibernateDao(CustomerHibernateDao customerHibernateDao) {
		this.customerHibernateDao = customerHibernateDao;
	}

public String updateCustomerStatusById(int id,int status) {
		Customer customer =getCustomerById(id);
		customer.setStatus(status);
		if(customerHibernateDao.updateCustomer(customer)){
			return "success";
		}
		return "fail";
	}

	public String updateCustomerPoint(String  userName, int point) {
    Customer customer = getCustomerByName(userName);
    customer.setPoint(customer.getPoint()+point);
    if(customerHibernateDao.updateCustomer(customer)){
		return "success";
	}
	return "fail";
}
	public String addCustomer(Customer customer) {
		if(getCustomerByName(customer.getUserName())!=null){
			return "have";//表示该用户名已经存在
		}
		if(customerHibernateDao.addCustomer(customer)){
			return "success";
		}
		return "fail";
	}

	public String deleteCustomerById(int id) {
		Customer customer = customerHibernateDao.getCustomerById(id);
		if(customer!=null){
			customerHibernateDao.deleteCustomer(customer);
			return "success";
		}
		return "fail";
	}

	public List<Customer> getAll() {
		return customerHibernateDao.getAll();
	}

	public Customer getCustomerById(int id) {
		return customerHibernateDao.getCustomerById(id);
	}

	public Customer getCustomerByName(String userName) {
		return customerHibernateDao.getCustomerByName(userName);
	}

	public PageContext getEntityPage(PageContext pageContext,
			Map<String, String> queryConditions, String orderProperty,
			String orderMode) {
		SimpleQuery query = customerDao.getSimpleQuery();
    	Page page = customerDao.find(query, pageContext.getPageNumber(), pageContext.getPageSize());
    	pageContext.setPage(page);
    	return pageContext;
	}

	public boolean support(String entityName) {
		// TODO Auto-generated method stub
		return false;
	}
	
	

}
